﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_Expert")]
    public class T_Expert : IEntity<T_Expert>
    {
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        [Key]
        public string UserName { get; set; }

        /// <summary>
        /// 称谓
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string Title { get; set; }

        /// <summary>
        /// 学位
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string Degree{get;set;}

        /// <summary>
        /// 擅长领域
        /// </summary>
        [Column(TypeName="int")]
        public int Profession { get; set; }
        /// <summary>
        /// 机构单位
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Institution { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(32)]
        public string Country { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Address { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(8)]
        public string Postcode { get; set; }

        [Column(TypeName="varchar")]
        [StringLength(32)]
        public string Fax { get; set; }

        [Column(TypeName="text")]
        public string Introduction { get; set; }

        [Column(TypeName="bit")]
        public bool Isvalid { get; set; }

        public override T_Expert Find(string username)
        {
            return Find(c => c.UserName == username);
        }

        public override T_Expert Find(System.Linq.Expressions.Expression<Func<T_Expert, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Expert.FirstOrDefault(where);
                if (item != null)
                {
                    this.Address = item.Address;
                    this.Country = item.Country;
                    this.Degree = item.Degree;
                    this.Fax = item.Fax;
                    this.Institution = item.Institution;
                    this.Introduction = item.Introduction;
                    this.Isvalid = item.Isvalid;
                    this.Postcode = item.Postcode;
                    this.Title = item.Title;
                    this.UserName = item.UserName;
                    this.Profession = item.Profession;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Expert, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Expert.Any(where);
            }
        }
    }
}
